--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[

    3.2、【过载充能】非节能模式下，饱食度超过70点时，搬运重物不再减速

]]--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- hook fn
   local hook_fn = function(self)

        self.___loramia_speed_mult_override_fns = {}
        self.___loramia_temp_inst_remove_event = function(temp_inst)
            self:loramia_RemoveSpeedMultOverrideFn(temp_inst)
        end

        self.loramia_AddSpeedMultOverrideFn = function(self,temp_inst,fn)
            if self.___loramia_speed_mult_override_fns[temp_inst] == nil then
                temp_inst:ListenForEvent("onremove",self.___loramia_temp_inst_remove_event)
            end
            self.___loramia_speed_mult_override_fns[temp_inst] = fn
        end
        self.loramia_RemoveSpeedMultOverrideFn = function(self,temp_inst)
            if self.___loramia_speed_mult_override_fns[temp_inst] == nil then
                return
            end
            temp_inst:RemoveEventCallback("onremove",self.___loramia_temp_inst_remove_event)
            local new_table = {}
            for k,v in pairs(self.___loramia_speed_mult_override_fns) do
                if k ~= temp_inst then
                    new_table[k] = v
                end
                self.___loramia_speed_mult_override_fns = new_table
            end
        end

        self.inst:DoTaskInTime(1,function()
            local old_GetSpeedMultiplier = self.GetSpeedMultiplier
            self.GetSpeedMultiplier = function(self)
                local result = old_GetSpeedMultiplier(self)
                for temp_inst, fn in pairs(self.___loramia_speed_mult_override_fns) do
                    result = fn(self.inst,result,temp_inst) or result
                end
                return result
            end
        end)
        
    end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---- hook and talent 
    local function talent_logic_install(inst)
        inst.components.locomotor:loramia_AddSpeedMultOverrideFn(inst,function(inst,result,temp_inst)
            if inst.replica.rider:IsRiding() then
                return result
            end
            if result < 1 then
                if inst.replica.loramia_com_talent_tree and inst.replica.loramia_com_talent_tree:IsUnlocked("overload_charging") -- 天赋
                   and inst.replica.hunger and inst.replica.hunger:GetCurrent() >= 70   -- 饱食度
                   and inst.replica.loramia_com_spell_controller and not inst.replica.loramia_com_spell_controller:NetGetData("energy_saving_mode") -- 非节能模式
                then
                    return 1
                end
            end
            return result
        end)
    end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

return function(inst)
    inst:DoTaskInTime(0,function(inst)
        if inst.components.locomotor then
            hook_fn(inst.components.locomotor)
            talent_logic_install(inst)
        end
    end)
    if not TheWorld.ismastersim then
        return
    end

end